<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Url;
use think\Session;
use think\Controller;

/**
 * 
 */
class Stock extends Base
{
	/*辅料库存*/
	public function accessories()
	{
		return $this->fetch();
	}
	/*成衣库存*/
	public function clothes()
	{
		return $this->fetch();
	}
	/*面料库存*/	
	public function fabric()
	{	
		if(Request::instance()->isPost())//搜索  ：   按照面料编号搜索
		{
			$name = Request::instance()->post('name');
			$where['ml_num'] = array('like',"%$name%");
		}
		$where['is_del'] = 0;
		$list = DB::name('fabric')->where($where)->order('id desc')->paginate(15);
		$page = $list->render();
		$list = $list->all();
		foreach ($list as $k => $v) {
			$list[$k]['category'] = DB::name('clothes_category')->where('id ='.$v['category'])->value('value');
			$list[$k]['color']	  = DB::name('clothes_color')->where('id = '.$v['color'])->value('value');
		}
		return $this->fetch('fabric',[
			'list'		=>	$list,
			'page'		=>	$page,
		]);
	}
	/*添加面料*/
	public function fabricCreate()
	{
		if(Request::instance()->isPost())
		{
			$data = Request::instance()->post();
			$data['all_num'] = $data['kucun'];
			$data['entry_id'] = Session::get('manage_id');
			$data['addtime']  = time();
			/*检测面料编号是否重复*/
			$res = DB::name('fabric')->where("ml_num = '".$data['ml_num']."' and is_del = 0")->find();
			if($res)/*有说明重复了*/
			{
				$this->error('面料编号错误');
			}else{/*没有重复  插入数据*/
				$result = DB::name('fabric')->insert($data);
				if($result){
					$this->success('面料录入成功',url('index/index/index',array('con'=>"stock",'act'=>'fabric')));
				}else{
					$this->wrror('面料录入失败');
				}
			}
		}
		$color = DB::name('clothes_color')->order('id asc')->select();
		$category = DB::name('clothes_category')->order('id asc')->select();
		return $this->fetch('fabric_create',[
			'color'		=>	$color,
			'category'	=>	$category,
		]);
	}
	/*修改面料属性*/
	public function fabricUpdate($id)
	{
		if(Request::instance()->isPost())
		{
			$data = Request::instance()->post();
			unset($data['id']);
			$res = DB::name('fabric')->where('id ='.$id)->setField($data);
			if($res)
			{
				$this->success('修改成功',url('index/index/index',array('con'=>'stock','act'=>'fabric')));
			}else{
				$this->error('修改失败');
			}
		}
		$color = DB::name('clothes_color')->order('id asc')->select();
		$category = DB::name('clothes_category')->order('id asc')->select();
		$data = DB::name('fabric')->where('id = '.$id)->find();
		return $this->fetch('fabric_create',[
			'color'		=>	$color,
			'category'	=>	$category,
			'data'		=>	$data,
		]);
	}
	/*添加面料库存*/
	public function fabricAddnum()
	{
		if(Request::instance()->isPost())
		{
			$data['num'] = Request::instance()->post('num');
			$id = Request::instance()->post('code');
			$data['ml_num'] = DB::name('fabric')->where('id ='.$id)->value('ml_num');

			$data['entry_id'] = Session::get('manage_id');
			$data['addtime'] = time();
			$res = DB::name('fabric_row')->insert($data);
			if($res){
				$r = DB::name('fabric')->where('id = '.$id)->setInc('kucun',$data['num']);
				$e = DB::name('fabric')->where('id = '.$id)->setInc('all_num',$data['num']);
				if($r && $e)
				{
					$this->success('库存添加成功',url('index/index/index',array('con'=>'stock','act'=>'fabric')));
				}
			}
		}
	}
	/*修改状态的方法*/
	public function updateStatus()
	{
		if(Request::instance()->isPost())
		{
			$id = Request::instance()->post('id');
			$data['status'] = Request::instance()->post('value');
			$res = DB::name('fabric')->where('id = '.$id)->setField($data);
			if($res){
				return 1;
			}else{
				return 2;
			}
		}
	}
	//* 删除面料库存  只修改状态  不删除数据*/
	public function delfabric()
	{
		if(Request::instance()->isPost())
		{
			$id = Request::instance()->post('id');
			/*判断权限*/
			$authmenu = new \think\Authmenu();
			$uid = Session::get('manage_id');
			$auth=$authmenu->check($uid,'clothes_del');
			if($auth !=true){//没有删除菜单的权限
				$res['sta'] = '-1';
				$res['msg'] = '权限不足';
			}else{
				$del = DB::name('fabric')->where('id = '.$id)->setField('is_del',1);
				if($del){
					$res['sta'] = '1';
					$res['msg'] = '删除成功';
				}else{
					$res['sta'] = '0';
					$res['msg'] = '删除失败';
				}
			}
			return json($res);
		}
	}
}
?>